package com.haitonghop.modules.truyentranh;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haitonghop.R;
import com.haitonghop.controls.AdsViewControl;
import com.haitonghop.data.TamDiemDetailModuleData;
import com.haitonghop.main.AdsBussiness;
import com.haitonghop.modules.tamdiem.TamDiemDetailModulesRenderAdapter;
import com.haitonghop.utils.sqllite.datasource.TruyenTranhBookmark;

public class TruyenTranhBookmarkActivity extends Activity implements OnItemClickListener {

	LinearLayout loadingPage = null;
	LinearLayout loadingError = null;

	LinearLayout mainContent = null;
	AdsViewControl prviewTamDiem = null;
	LinearLayout loadingMore = null;
	GridView imageListView = null;

	TextView errorMessageView = null;
	TamDiemDetailModulesRenderAdapter tamDiemRenderAdapter = null;
	String idModules = "";
	String nameTitle = "";
	boolean isLoading = false;

	long requestId = -1;
	int MAX_COUNT = 20;

	private ArrayList<TruyenTranhBookmark> bookmarks;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		bookmarks = TruyenTranhSession.getBookmark(this);

		// Remove notification bar
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.truyentranh_detail_module_activity_layout);
		init();

		loadingPage = (LinearLayout) findViewById(R.id.loadingpagetamdiem);
		loadingError = (LinearLayout) findViewById(R.id.loadingError);
		loadingMore = (LinearLayout) findViewById(R.id.loadMore);
		loadingError.setVisibility(View.INVISIBLE);
		errorMessageView = (TextView) findViewById(R.id.errorMessageView);
		mainContent = (LinearLayout) findViewById(R.id.mainContent);

		prviewTamDiem = (AdsViewControl) findViewById(R.id.prviewTamDiem);
		imageListView = (GridView) findViewById(R.id.imageListView);

		imageListView.setNumColumns(3);

		TextView textView = (TextView) findViewById(R.id.moduleName);
		textView.setText("Truyện đã đọc");

		errorMessageView.setText("Bạn chưa đánh dấu truyện khi xem, hãy vào KHO TRUYỆN, chỉ cần bạn đọc dang dở 1 truyện nào đó, nó sẽ được đánh dấu lại, lần sau chỉ việc mở lên đọc tiếp thôi ^^");
		ImageButton button = (ImageButton) findViewById(R.id.menu_button_right);
		button.setVisibility(View.GONE);
	}

	void doAdsByServer() {
		AdsBussiness.getInstance().doGetAdsFromServer(this, prviewTamDiem);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			requestId = -1;
			finish();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	boolean isStart = true;

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		if (isStart) {
			isStart = false;
			onStartUp();
		} else {
			doAdsByServer();
		}
	}

	int page = 1;

	private void onStartUp() {

		loadingPage.setVisibility(View.VISIBLE);

		loadingError.setVisibility(View.INVISIBLE);
		loadingMore.setVisibility(View.INVISIBLE);
		loadingError.setVisibility(View.INVISIBLE);
		mainContent.setVisibility(View.INVISIBLE);
		prviewTamDiem.setVisibility(View.VISIBLE);
		imageListView.setVisibility(View.INVISIBLE);

		ArrayList<TamDiemDetailModuleData> cachedList = new ArrayList<TamDiemDetailModuleData>();
		if (bookmarks != null) {
			for (int i = 0, n = bookmarks.size(); i < n; i++) {
				TamDiemDetailModuleData data = bookmarks.get(i).getCurrentComic();
				boolean isfound = false;
				for (int iter = 0; iter < cachedList.size(); iter++) {
					TamDiemDetailModuleData tmp = cachedList.get(iter);
					if (tmp.id().equals(data.id())) {
						isfound = true;
						break;
					}
				}

				if (!isfound) {
					cachedList.add(data);
				}
			}
		}
		if (cachedList != null) {
			fillDatatoGridView(cachedList);
		}
	}

	void init() {
	}

	void fillDatatoGridView(final ArrayList<TamDiemDetailModuleData> requestedDataList) {

		if (imageListView != null && requestedDataList != null && !requestedDataList.isEmpty()) {
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					// DataCachedControl.getInstance().putCached(DataCachedControl.TRUYENTRANH_CATEGORY_DETAIL,
					// new
					// ArrayList<TamDiemDetailModuleData>(requestedDataList));
					if (tamDiemRenderAdapter == null) {
						tamDiemRenderAdapter = new TamDiemDetailModulesRenderAdapter(TruyenTranhBookmarkActivity.this, 1, 1, requestedDataList);
						imageListView.setAdapter(tamDiemRenderAdapter);
					} else {
						tamDiemRenderAdapter.setBlockList(requestedDataList);
						tamDiemRenderAdapter.notifyDataSetChanged();
					}
					tamDiemRenderAdapter.truyentranh = true;
					imageListView.setOnItemClickListener(TruyenTranhBookmarkActivity.this);

					loadingPage.setVisibility(View.GONE);
					loadingError.setVisibility(View.GONE);
					loadingMore.setVisibility(View.GONE);

					mainContent.setVisibility(View.VISIBLE);
					prviewTamDiem.setVisibility(View.VISIBLE);
					imageListView.setVisibility(View.VISIBLE);

					isLoading = false;
					doAdsByServer();
				}
			});

		} else {
			loadingPage.setVisibility(View.GONE);
			loadingError.setVisibility(View.VISIBLE);
			loadingMore.setVisibility(View.GONE);

			mainContent.setVisibility(View.GONE);
			prviewTamDiem.setVisibility(View.VISIBLE);
			imageListView.setVisibility(View.GONE);

			isLoading = false;
			doAdsByServer();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int index, long arg3) {

		if (tamDiemRenderAdapter != null) {
			TamDiemDetailModuleData diemMetalData = tamDiemRenderAdapter.getItem(index);

			final String href = diemMetalData.getHref();
			if (href != null && !"".equals(href)) {
				Intent intent = new Intent(getApplication(), TruyenTranhChapterListActivity.class);
				intent.putExtra("href", href);
				intent.putExtra("NAME", diemMetalData.getTitle());
				intent.putExtra("AVATAR", diemMetalData.getAvatar());
				TruyenTranhSession.setCurrentComic(diemMetalData);

				startActivity(intent);
			}
		}
	}

}
